package com.xu.sso.db.service.impl;

import com.xu.sso.core.enums.StatusEnum;
import com.xu.sso.db.dao.AppRoleExMapper;
import com.xu.sso.db.dao.AppUserRoleExMapper;
import com.xu.sso.db.dao.ResourcesRoleExMapper;
import com.xu.sso.db.model.base.*;
import com.xu.sso.db.service.RoleDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleDBServiceImpl implements RoleDBService {

    @Autowired
    private AppRoleExMapper appRoleExMapper;
    @Autowired
    private AppUserRoleExMapper appUserRoleExMapper;
    @Autowired
    private ResourcesRoleExMapper resourcesRoleExMapper;

    @Override
    public List<AppRole> findRolesByApp(String appId) {
        AppRoleExample example = new AppRoleExample();
        example.createCriteria().andAppIdEqualTo(appId);
        return appRoleExMapper.selectByExample(example);
    }

    @Override
    public List<AppRole> findInAppRoles(String appId, String username) {
        AppUserRoleExample example = new AppUserRoleExample();
        example.createCriteria().andUsernameEqualTo(username).andAppIdEqualTo(appId);
        List<AppUserRole> appUserRoles = appUserRoleExMapper.selectByExample(example);
        if (null == appUserRoles || appUserRoles.size() == 0) {
            return new ArrayList<>();
        }
        List<Integer> roleIds = appUserRoles.stream().map(AppUserRole::getRoleId).collect(Collectors.toList());
        AppRoleExample example1 = new AppRoleExample();
        example1.createCriteria().andAppIdEqualTo(appId).andStatusEqualTo(StatusEnum.Start.getCode()).andIdIn(roleIds);
        return appRoleExMapper.selectByExample(example1);
    }

    @Override
    public List<AppRole> findNotInAppRoles(String appId, String username) {
        AppUserRoleExample example = new AppUserRoleExample();
        example.createCriteria().andUsernameEqualTo(username).andAppIdEqualTo(appId);
        List<AppUserRole> appUserRoles = appUserRoleExMapper.selectByExample(example);
        List<Integer> roleIds = appUserRoles.stream().map(AppUserRole::getRoleId).collect(Collectors.toList());
        AppRoleExample example1 = new AppRoleExample();
        AppRoleExample.Criteria criteria = example1.createCriteria().andAppIdEqualTo(appId).andStatusEqualTo(StatusEnum.Start.getCode());
        if (roleIds.size() > 0) {
            criteria.andIdNotIn(roleIds);
        }
        return appRoleExMapper.selectByExample(example1);
    }

    @Override
    public List<AppRole> findInResRolesByResId(String appId, Integer resId) {
        ResourcesRoleExample example = new ResourcesRoleExample();
        example.createCriteria().andAppIdEqualTo(appId).andResIdEqualTo(resId);
        List<ResourcesRole> resourcesRoles = resourcesRoleExMapper.selectByExample(example);
        if (null == resourcesRoles || resourcesRoles.size() == 0) {
            return new ArrayList<>();
        }
        List<Integer> roleIds = resourcesRoles.stream().map(ResourcesRole::getRoleId).collect(Collectors.toList());
        AppRoleExample example1 = new AppRoleExample();
        example1.createCriteria().andAppIdEqualTo(appId).andStatusEqualTo(StatusEnum.Start.getCode()).andIdIn(roleIds);
        return appRoleExMapper.selectByExample(example1);

    }

    @Override
    public List<AppRole> findNotInResRolesByResId(String appId, Integer resId) {
        ResourcesRoleExample example = new ResourcesRoleExample();
        example.createCriteria().andAppIdEqualTo(appId).andResIdEqualTo(resId);
        List<ResourcesRole> resourcesRoles = resourcesRoleExMapper.selectByExample(example);
        List<Integer> roleIds = resourcesRoles.stream().map(ResourcesRole::getRoleId).collect(Collectors.toList());
        AppRoleExample example1 = new AppRoleExample();
        AppRoleExample.Criteria criteria = example1.createCriteria().andAppIdEqualTo(appId).andStatusEqualTo(StatusEnum.Start.getCode());
        if (roleIds.size() > 0) {
            criteria.andIdNotIn(roleIds);
        }
        return appRoleExMapper.selectByExample(example1);

    }

    @Override
    public AppRole findAppRoleById(Integer id) {
        return appRoleExMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkAppRoleExist(String appId, String roleName) {
        AppRoleExample example = new AppRoleExample();
        example.createCriteria().andAppIdEqualTo(appId).andRoleNameEqualTo(roleName);
        List<AppRole> appRoles = appRoleExMapper.selectByExample(example);
        if (null != appRoles && appRoles.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int createAppRole(AppRole appRole) {
        appRole.setUpdateTime(new Date());
        appRole.setCreateTime(new Date());
        return appRoleExMapper.insertSelective(appRole);
    }

    @Override
    public int createAppUserRoles(List<Integer> roleIds, String appId, String username) {
        return appUserRoleExMapper.insertAppUserRolesByList(roleIds, appId, username);
    }

    @Override
    public int createResourcesRoles(List<Integer> roleIds, String appId, Integer resId) {
        return resourcesRoleExMapper.insertResourcesRoles(roleIds, appId, resId);
    }

    @Override
    public int deleteAppRoleById(Integer id) {
        return appRoleExMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteAppRoleByApp(String appId) {
        AppRoleExample example = new AppRoleExample();
        example.createCriteria().andAppIdEqualTo(appId);
        return appRoleExMapper.deleteByExample(example);
    }

    @Override
    public int deleteAppUserRoles(List<Integer> roleIds, String appId, String username) {
        AppUserRoleExample example = new AppUserRoleExample();
        example.createCriteria().andAppIdEqualTo(appId).andUsernameEqualTo(username).andRoleIdIn(roleIds);
        return appUserRoleExMapper.deleteByExample(example);
    }

    @Override
    public int deleteAppUserRolesByApp(String appId) {
        AppUserRoleExample example = new AppUserRoleExample();
        example.createCriteria().andAppIdEqualTo(appId);
        return appUserRoleExMapper.deleteByExample(example);
    }

    @Override
    public int deleteAppUserRolesByAppUser(String appId, String username) {
        AppUserRoleExample example = new AppUserRoleExample();
        example.createCriteria().andAppIdEqualTo(appId).andUsernameEqualTo(username);
        return appUserRoleExMapper.deleteByExample(example);
    }

    @Override
    public int deleteAppUserRolesByUser(String username) {
        AppUserRoleExample example = new AppUserRoleExample();
        example.createCriteria().andUsernameEqualTo(username);
        return appUserRoleExMapper.deleteByExample(example);
    }

    @Override
    public int deleteAppUserRolesByRoleId(Integer roleId) {
        AppUserRoleExample example = new AppUserRoleExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        return appUserRoleExMapper.deleteByExample(example);
    }

    @Override
    public int deleteResourcesRole(List<Integer> roleIds, String appId, Integer resId) {
        ResourcesRoleExample example = new ResourcesRoleExample();
        example.createCriteria().andAppIdEqualTo(appId).andResIdEqualTo(resId).andRoleIdIn(roleIds);
        return resourcesRoleExMapper.deleteByExample(example);
    }

    @Override
    public int deleteResourcesRoleByAppId(String appId) {
        ResourcesRoleExample example = new ResourcesRoleExample();
        example.createCriteria().andAppIdEqualTo(appId);
        return resourcesRoleExMapper.deleteByExample(example);
    }

    @Override
    public int deleteResourcesRoleByResId(Integer resId) {
        ResourcesRoleExample example = new ResourcesRoleExample();
        example.createCriteria().andResIdEqualTo(resId);
        return resourcesRoleExMapper.deleteByExample(example);
    }

    @Override
    public int deleteResourcesRoleByRoleId(Integer roleId) {
        ResourcesRoleExample example = new ResourcesRoleExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        return resourcesRoleExMapper.deleteByExample(example);
    }

    @Override
    public int updateAppRole(AppRole appRole) {
        appRole.setUpdateTime(new Date());
        return appRoleExMapper.updateByPrimaryKeySelective(appRole);
    }

    @Override
    public int updateAppRoleStatus(Integer id, String status) {
        AppRole appRole = new AppRole();
        appRole.setStatus(status);
        appRole.setId(id);
        appRole.setUpdateTime(new Date());
        return appRoleExMapper.updateByPrimaryKeySelective(appRole);
    }
}
